package com.multipleimageselect.adapters;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;

import com.asman.base.lib.glide.GlideApp;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jph.takephoto.R;
import com.multipleimageselect.activities.VideoSelectActivity;
import com.multipleimageselect.helpers.Constants;
import com.multipleimageselect.models.Image;

import java.io.File;
import java.util.List;

/**
 * Created by Darshan on 4/18/2015.
 */

public class CustomVideoSelectAdapter extends RecyclerView.Adapter<CustomVideoSelectAdapter.MyViewHolder> {

    private List<Image> data;

    public CustomVideoSelectAdapter(List<Image> data) {
//        super(R.layout.grid_view_item_album_select, albums);
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_item_video_select, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.setData(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView image_view_image_select;
        AppCompatCheckBox checkBox;
        TextView tv_duration;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            image_view_image_select = itemView.findViewById(R.id.image_view_image_select);
            checkBox = itemView.findViewById(R.id.checkBox);
            tv_duration = itemView.findViewById(R.id.tv_duration);
        }

        @SuppressLint("SetTextI18n")
        public void setData(Image item) {
            itemView.setOnClickListener(v -> {
                VideoSelectActivity activity = (VideoSelectActivity) itemView.getContext();
                activity.toggleSelection(item);
                activity.toolbar.setTitle(activity.countSelected + "/" + Constants.limit);
            });
            try {
                tv_duration.setText(getTimeFormat(Long.parseLong(item.duration) / 1000));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            checkBox.setChecked(item.isSelected);
            GlideApp.with(itemView.getContext())
                    .load(new File(item.path))
                    .apply(new RequestOptions().centerCrop().placeholder(new ColorDrawable(Color.parseColor("#f7f7f7"))).diskCacheStrategy(DiskCacheStrategy.NONE))
                    .into(image_view_image_select);
        }
    }

    public String getTimeFormat(Long second) {
        //33秒
        //1分钟3秒
        //1小时2分钟
        if (second < 60) {// < 1分钟
            return second + "秒";
        } else if (second == 60) {// == 1分钟
            return 1 + "分钟";
        } else if (second < 3600) {// < 1小时
            long minute = second / 60;//分钟
            long second1 = second % 60;//秒
            return minute + "分钟" + second1 + "秒";
        } else if (second == 3600) {
            return "1小时";
        } else {
            long hour = second / 3600;//小时
            long minute = second % 3600 / 60;//分钟
            return hour + "小时" + minute + "分钟";
        }
    }

}
